package com.tqjc.execute.task.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.area.entity.SteelPlate;
import com.tqjc.execute.task.entity.Task;
import com.tqjc.execute.task.mapper.TaskMapper;
import com.tqjc.execute.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 任务Service实现类
 * @author: pzy
 * @date: 2023年05月16日 15:47
 */

@Service
@DS("baotou")
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService{
    @Resource
    private TaskMapper taskMapper;

    @Override
    public Task getNearestTimeSuckedStatusByCraneType(String craneType) {
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        taskQueryWrapper.eq("crane_type", craneType);
        taskQueryWrapper.eq("task_status",1);
        taskQueryWrapper.orderByDesc("begin_time");
        List<Task> tasklist=list(taskQueryWrapper);
        if(tasklist!=null&&tasklist.size()>0){
            return tasklist.get(0);
        }
        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Task queryById(Integer id){
        return taskMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param task 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<Task> paginQuery(Task task, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(task.getCraneType())){
            queryWrapper.eq(Task::getCraneType, task.getCraneType());
        }
        if(StrUtil.isNotBlank(task.getType())){
            queryWrapper.eq(Task::getType, task.getType());
        }
        //2. 执行分页查询
        Page<Task> pagin = new Page<>(current , size , true);
        IPage<Task> selectResult = taskMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param task 实例对象
     * @return 实例对象
     */
    @Override
    public Task insert(Task task){
        taskMapper.insert(task);
        return task;
    }

    /**
     * 更新数据
     *
     * @param task 实例对象
     * @return 实例对象
     */
    @Override
    public Task update(Task task){

        taskMapper.update(task,null);
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<Task> chainWrapper = new LambdaUpdateChainWrapper<Task>(taskMapper);
        if(StrUtil.isNotBlank(task.getCraneType())){
            chainWrapper.eq(Task::getCraneType, task.getCraneType());
        }
        if(StrUtil.isNotBlank(task.getType())){
            chainWrapper.eq(Task::getType, task.getType());
        }
        //2. 设置主键，并更新
        chainWrapper.set(Task::getId, task.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(task.getId());
        }else{
            return task;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = taskMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public boolean isExistSemiAutoTask(String matNo, String craneNanme) {
        int integer = taskMapper.selectCount(new LambdaQueryWrapper<Task>().eq(Task::getSteelNo, matNo).eq(Task::getCraneType, craneNanme));
        if(integer > 0){
            return true;
        }
        return false;
    }
}